/**@file
   Log related functions.
   
   @par license 
   Copyright 2012 Lu, Ken. All rights reserved.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 
   @author: Lu, Ken (bluewish.ken.lu@gmail.com)
**/

#include <ntddk.h>
#include <wdf.h>
#include <stdio.h>
#include <stdarg.h>
#include "log.h"

static int g_level = KLOG_DEBUG;
static int g_verbose = TRUE;

void 
    kbl_log(
    int     level, 
    char    *function, 
    char    *file,
    int     line,
    char    *format, 
    ...
    )
{
    va_list marker;
    char buffer[256] = {0};
    char message[512] = {0};

    if (level < g_level) {
        return;
    }

    switch (level) {
    case KLOG_DEBUG:
        strcat_s(buffer, 4, "[D] ");
        break;
    case KLOG_INFO:
        strcat_s(buffer, 4, "[I] ");
        break;
    case KLOG_WARNING:
        strcat_s(buffer, 4, "[W] ");
        break;
    case KLOG_ERROR:
        strcat_s(buffer, 4, "[E] ");
        break;
    case KLOG_ASSERT:
        strcat_s(buffer, 4, "[A] ");
        break;
    default:
        return;
    }

    va_start(marker, format);
    vsprintf_s(&buffer[strlen(buffer)], (256 - strlen(buffer)), format, marker);
    va_end(marker);

    if (g_verbose) {
        sprintf_s(message, 512, "%s (%s: (%d))", buffer, file, line);
    } else {
        sprintf_s(message, 512, "%s %s", function, buffer);
    }

    KdPrint((message));
}

